{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import IFrame\n",
    "import ipynb_style\n",
    "from epstata import Stpy\n",
    "import pandas as pd\n",
    "from itertools import combinations\n",
    "from importlib import reload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<style type=\"text/css\">\n",
       "div.input {\n",
       "width: 105ex; /* about 80 chars + buffer */\n",
       "}\n",
       "\n",
       "div.text_cell {\n",
       "width: 105ex /* instead of 100%, */\n",
       "}\n",
       "\n",
       "div.text_cell_render {\n",
       "/*font-family: \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;*/\n",
       "font-family: \"Charis SIL\", serif; /* Make non-code text serif. */\n",
       "line-height: 145%; /* added for some line spacing of text. */\n",
       "width: 105ex; /* instead of 'inherit' for shorter lines */\n",
       "}\n",
       "\n",
       "/* Set the size of the headers */\n",
       "div.text_cell_render h1 {\n",
       "font-size: 18pt;\n",
       "}\n",
       "\n",
       "div.text_cell_render h2 {\n",
       "font-size: 14pt;\n",
       "}\n",
       "\n",
       ".CodeMirror {\n",
       " font-family: Consolas, monospace;\n",
       " }\n",
       "\n",
       ".rendered_html ol {list-style:decimal; margin: 1em 2em;}\n",
       "\n",
       "</style>\n",
       "<style>table.dataframe {\n",
       "border-collapse: collapse;\n",
       "border: none;\n",
       "font-size:100%;\n",
       "}</style>\n",
       "\n",
       "<style>table.dataframe tr {\n",
       "border: none;\n",
       "}\n",
       "table.dataframe td, table.dataframe th {\n",
       "margin: 0;\n",
       "border: 1px solid white;\n",
       "padding-left: 0.25em;\n",
       "padding-right: 0.25em;\n",
       "}\n",
       "table.dataframe th:not(:empty) {\n",
       "background-color: #fec;\n",
       "text-align: left;\n",
       "font-weight: normal;\n",
       "}\n",
       "table.dataframe tr:nth-child(2) th:empty {\n",
       "border-left: none;\n",
       "border-right: 1px dashed #888;\n",
       "}\n",
       "table.dataframe td {\n",
       "border: 2px solid #ccf;\n",
       "background-color: #f4f4ff;\n",
       "}</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reload(ipynb_style)\n",
    "ipynb_style.clean()\n",
    "#ipynb_style.presentation()\n",
    "#ipynb_style.pres2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Data Workflows in Stata and Python\n",
    "<a href=\"http://www.stata.com\"><img src=\"Stata_Logo.svg\" width=\"300\" height=\"150\" style=\"float: left; display: inline; margin: 10px\" alt=\"Stata\"></a>\n",
    "<a href=\"https://www.python.org\"><img src=\"python-logo-generic.svg\" width=\"300\" height=\"150\" style=\"float: left; display: inline; margin: 10px\" alt=\"Python\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Data Workflows in Stata and Python\n",
    "\n",
    "**Dejan Pavlic**, Education Policy Research Initiative, University of Ottawa\n",
    "\n",
    "**Stephen Childs** (presenter), Office of Institutional Analysis, University of Calgary\n",
    "\n",
    "<a href=\"http://ucalgary.ca\"><img src=\"uc-vert-rgb.png\" width=\"173\" height=\"129\" style=\"float: left; display: inline; margin: 10px\" alt=\"University of Calgary\"></a>\n",
    "<a href=\"http://uottawa.ca/en\"><img src=\"uottawa_ver_wg9.png\" width=\"152\" height=\"129\" style=\"float: left; display: inline; margin: 25px\" alt=\"University of Ottawa\"></a>\n",
    "<a href=\"http://socialsciences.uottawa.ca/irpe-epri/eng/index.asp\"><img src=\"epri_logo.jpg\" width=\"524\" height=\"129\" style=\"float: left; display: inline; margin: 0px\" alt=\"Education Policy Research Initiative\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## About this talk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Objectives\n",
    "\n",
    "* know what Python is and what advantages it has\n",
    "* know how Python can work with Stata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Please save questions for the end.** Or feel free to ask me today or after the conference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Outline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Introduction\n",
    "    * Overall\n",
    "    * Motivation\n",
    "    * About Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Building Blocks\n",
    "    * Running Stata from Python\n",
    "    * Pandas\n",
    "    * Python language features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "* Workflows\n",
    "    * ETL/Data Cleaning\n",
    "    * Stata code generation\n",
    "    * Processing Stata output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## About Me\n",
    "\n",
    "* Started using Stata in grad school (2006).\n",
    "* Using Python for about 3 years.\n",
    "* Post-Secondary Education sector\n",
    "    * University of Calgary - [Institutional Analysis](https://oia.ucalgary.ca/Contact)\n",
    "    * [Education Policy Research Initiative](http://socialsciences.uottawa.ca/irpe-epri/eng/index.asp) - University of Ottawa (a Stata shop)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Motivation\n",
    "\n",
    "* Python is becoming very popular in the data world.\n",
    "* Python skills are widely applicable.\n",
    "* Python is powerful and flexible and will help you get more done, faster."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## About Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### The Python Language\n",
    "\n",
    "* General purpose programming language\n",
    "* Name comes from Monty Python\n",
    "* Python 2 vs. 3 - use Python 3\n",
    "* \"batteries included\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Scientific Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<a href=\"http://pandas.pydata.org\"><img src=\"pandas_logo.png\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<a href=\"http://matplotlib.org/\"><img src=\"matplotlib_logo.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<a href=\"http://www.numpy.org\"><img src=\"numpy_logo.png\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "<center><h2><a href=\"http://scipy.org\"><img src=\"scipyshiny_small.png\" style=\"display: inline;\"></a>SciPy</h2></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<a href=\"https://jupyter.org/\"><img src=\"jupyter-sq-text.svg\" height=200 width=200></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<a href=\"http://continuum.io/downloads\"><img src=\"anaconda_logo_web.png\"></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Building Blocks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Stata Commands from Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Use the Stata command line\n",
    "* Python's `subprocess` module runs each instance of Stata\n",
    "* Each instance is a Python object\n",
    "* Can send it commands with the `write()` method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  ___  ____  ____  ____  ____ (R)\n",
      " /__    /   ____/   /   ____/\n",
      "___/   /   /___/   /   /___/   13.1   Copyright 1985-2013 StataCorp LP\n",
      "  Statistics/Data Analysis            StataCorp\n",
      "                                      4905 Lakeway Drive\n",
      "     MP - Parallel Edition            College Station, Texas 77845 USA\n",
      "                                      800-STATA-PC        http://www.stata.com\n",
      "                                      979-696-4600        stata@stata.com\n",
      "                                      979-696-4601 (fax)\n",
      "\n",
      "2-user 2-core Stata network perpetual license:\n",
      "       Serial number:  501306211345\n",
      "         Licensed to:  Stephen Childs\n",
      "                       Education Policy Research Initiative\n",
      "\n",
      "Notes:\n",
      "      1.  (-v# option or -set maxvar-) 5000 maximum variables\n",
      "      2.  Command line editing disabled\n",
      "      3.  Stata running in batch mode\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata = Stpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sysuse auto\n",
      "(1978 Automobile Data)\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('sysuse auto')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "describe\n",
      "\n",
      "Contains data from /Applications/Stata/ado/base/a/auto.dta\n",
      "  obs:            74                          1978 Automobile Data\n",
      " vars:            12                          13 Apr 2013 17:45\n",
      " size:         3,182                          (_dta has notes)\n",
      "-------------------------------------------------------------------------------\n",
      "              storage   display    value\n",
      "variable name   type    format     label      variable label\n",
      "-------------------------------------------------------------------------------\n",
      "make            str18   %-18s                 Make and Model\n",
      "price           int     %8.0gc                Price\n",
      "mpg             int     %8.0g                 Mileage (mpg)\n",
      "rep78           int     %8.0g                 Repair Record 1978\n",
      "headroom        float   %6.1f                 Headroom (in.)\n",
      "trunk           int     %8.0g                 Trunk space (cu. ft.)\n",
      "weight          int     %8.0gc                Weight (lbs.)\n",
      "length          int     %8.0g                 Length (in.)\n",
      "turn            int     %8.0g                 Turn Circle (ft.)\n",
      "displacement    int     %8.0g                 Displacement (cu. in.)\n",
      "gear_ratio      float   %6.2f                 Gear Ratio\n",
      "foreign         byte    %8.0g      origin     Car type\n",
      "-------------------------------------------------------------------------------\n",
      "Sorted by:  foreign\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('describe')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Python strings have a `format()` method that allows you to substitute the contents of Python variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "depvar = 'mpg'\n",
    "indepvars = ['weight', 'wtsq', 'foreign']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'regress mpg weight wtsq foreign'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'regress {depvar} {indepvars}'.format(depvar=depvar,\n",
    "                                      indepvars=' '.join(indepvars))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* General introduction\n",
    "    * Origins - NumPy\n",
    "    * Current popularity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Key concepts\n",
    "    * DataFrame\n",
    "    * Series\n",
    "    * index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I will use the `sysuse auto` dataset to demonstrate some basic functions with Pandas. This is taken from the Stata tutorial and reflects basic commands for exploring and manipulating your data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Pandas and Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "auto = pd.read_stata('auto.dta')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Overall Dataset Description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(74, 12)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "make              object\n",
       "price              int16\n",
       "mpg                int16\n",
       "rep78            float64\n",
       "headroom         float32\n",
       "trunk              int16\n",
       "weight             int16\n",
       "length             int16\n",
       "turn               int16\n",
       "displacement       int16\n",
       "gear_ratio       float32\n",
       "foreign         category\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Overall Summary Statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "summarize\n",
      "\n",
      "    Variable |       Obs        Mean    Std. Dev.       Min        Max\n",
      "-------------+--------------------------------------------------------\n",
      "        make |         0\n",
      "       price |        74    6165.257    2949.496       3291      15906\n",
      "         mpg |        74     21.2973    5.785503         12         41\n",
      "       rep78 |        69    3.405797    .9899323          1          5\n",
      "    headroom |        74    2.993243    .8459948        1.5          5\n",
      "-------------+--------------------------------------------------------\n",
      "       trunk |        74    13.75676    4.277404          5         23\n",
      "      weight |        74    3019.459    777.1936       1760       4840\n",
      "      length |        74    187.9324    22.26634        142        233\n",
      "        turn |        74    39.64865    4.399354         31         51\n",
      "displacement |        74    197.2973    91.83722         79        425\n",
      "-------------+--------------------------------------------------------\n",
      "  gear_ratio |        74    3.014865    .4562871       2.19       3.89\n",
      "     foreign |        74    .2972973    .4601885          0          1\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('summarize')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>price</th>\n",
       "      <td>74</td>\n",
       "      <td>6165.256757</td>\n",
       "      <td>2949.495885</td>\n",
       "      <td>3291.00</td>\n",
       "      <td>4220.25</td>\n",
       "      <td>5006.500</td>\n",
       "      <td>6332.2500</td>\n",
       "      <td>15906.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mpg</th>\n",
       "      <td>74</td>\n",
       "      <td>21.297297</td>\n",
       "      <td>5.785503</td>\n",
       "      <td>12.00</td>\n",
       "      <td>18.00</td>\n",
       "      <td>20.000</td>\n",
       "      <td>24.7500</td>\n",
       "      <td>41.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rep78</th>\n",
       "      <td>69</td>\n",
       "      <td>3.405797</td>\n",
       "      <td>0.989932</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>3.000</td>\n",
       "      <td>4.0000</td>\n",
       "      <td>5.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>headroom</th>\n",
       "      <td>74</td>\n",
       "      <td>2.993243</td>\n",
       "      <td>0.845995</td>\n",
       "      <td>1.50</td>\n",
       "      <td>2.50</td>\n",
       "      <td>3.000</td>\n",
       "      <td>3.5000</td>\n",
       "      <td>5.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trunk</th>\n",
       "      <td>74</td>\n",
       "      <td>13.756757</td>\n",
       "      <td>4.277404</td>\n",
       "      <td>5.00</td>\n",
       "      <td>10.25</td>\n",
       "      <td>14.000</td>\n",
       "      <td>16.7500</td>\n",
       "      <td>23.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>74</td>\n",
       "      <td>3019.459459</td>\n",
       "      <td>777.193567</td>\n",
       "      <td>1760.00</td>\n",
       "      <td>2250.00</td>\n",
       "      <td>3190.000</td>\n",
       "      <td>3600.0000</td>\n",
       "      <td>4840.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>74</td>\n",
       "      <td>187.932432</td>\n",
       "      <td>22.266340</td>\n",
       "      <td>142.00</td>\n",
       "      <td>170.00</td>\n",
       "      <td>192.500</td>\n",
       "      <td>203.7500</td>\n",
       "      <td>233.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>turn</th>\n",
       "      <td>74</td>\n",
       "      <td>39.648649</td>\n",
       "      <td>4.399354</td>\n",
       "      <td>31.00</td>\n",
       "      <td>36.00</td>\n",
       "      <td>40.000</td>\n",
       "      <td>43.0000</td>\n",
       "      <td>51.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>displacement</th>\n",
       "      <td>74</td>\n",
       "      <td>197.297297</td>\n",
       "      <td>91.837219</td>\n",
       "      <td>79.00</td>\n",
       "      <td>119.00</td>\n",
       "      <td>196.000</td>\n",
       "      <td>245.2500</td>\n",
       "      <td>425.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>gear_ratio</th>\n",
       "      <td>74</td>\n",
       "      <td>3.014865</td>\n",
       "      <td>0.456287</td>\n",
       "      <td>2.19</td>\n",
       "      <td>2.73</td>\n",
       "      <td>2.955</td>\n",
       "      <td>3.3525</td>\n",
       "      <td>3.89</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              count         mean          std      min      25%       50%  \\\n",
       "price            74  6165.256757  2949.495885  3291.00  4220.25  5006.500   \n",
       "mpg              74    21.297297     5.785503    12.00    18.00    20.000   \n",
       "rep78            69     3.405797     0.989932     1.00     3.00     3.000   \n",
       "headroom         74     2.993243     0.845995     1.50     2.50     3.000   \n",
       "trunk            74    13.756757     4.277404     5.00    10.25    14.000   \n",
       "weight           74  3019.459459   777.193567  1760.00  2250.00  3190.000   \n",
       "length           74   187.932432    22.266340   142.00   170.00   192.500   \n",
       "turn             74    39.648649     4.399354    31.00    36.00    40.000   \n",
       "displacement     74   197.297297    91.837219    79.00   119.00   196.000   \n",
       "gear_ratio       74     3.014865     0.456287     2.19     2.73     2.955   \n",
       "\n",
       "                    75%       max  \n",
       "price         6332.2500  15906.00  \n",
       "mpg             24.7500     41.00  \n",
       "rep78            4.0000      5.00  \n",
       "headroom         3.5000      5.00  \n",
       "trunk           16.7500     23.00  \n",
       "weight        3600.0000   4840.00  \n",
       "length         203.7500    233.00  \n",
       "turn            43.0000     51.00  \n",
       "displacement   245.2500    425.00  \n",
       "gear_ratio       3.3525      3.89  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.describe().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### View the first observations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list make price mpg rep78 foreign in 1/5\n",
      "\n",
      "     +------------------------------------------------+\n",
      "     | make            price   mpg   rep78    foreign |\n",
      "     |------------------------------------------------|\n",
      "  1. | AMC Concord     4,099    22       3   Domestic |\n",
      "  2. | AMC Pacer       4,749    17       3   Domestic |\n",
      "  3. | AMC Spirit      3,799    22       .   Domestic |\n",
      "  4. | Buick Century   4,816    20       3   Domestic |\n",
      "  5. | Buick Electra   7,827    15       4   Domestic |\n",
      "     +------------------------------------------------+\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('list make price mpg rep78 foreign in 1/5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>make</th>\n",
       "      <th>price</th>\n",
       "      <th>mpg</th>\n",
       "      <th>rep78</th>\n",
       "      <th>foreign</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AMC Concord</td>\n",
       "      <td>4099</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AMC Pacer</td>\n",
       "      <td>4749</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AMC Spirit</td>\n",
       "      <td>3799</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Buick Century</td>\n",
       "      <td>4816</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Buick Electra</td>\n",
       "      <td>7827</td>\n",
       "      <td>15</td>\n",
       "      <td>4</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            make  price  mpg  rep78   foreign\n",
       "0    AMC Concord   4099   22      3  Domestic\n",
       "1      AMC Pacer   4749   17      3  Domestic\n",
       "2     AMC Spirit   3799   22    NaN  Domestic\n",
       "3  Buick Century   4816   20      3  Domestic\n",
       "4  Buick Electra   7827   15      4  Domestic"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto[['make', 'price', 'mpg', 'rep78', 'foreign']].head(5) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**What about listing the first 5 foreign cars...**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>make</th>\n",
       "      <th>price</th>\n",
       "      <th>mpg</th>\n",
       "      <th>rep78</th>\n",
       "      <th>foreign</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>Audi 5000</td>\n",
       "      <td>9690</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>Audi Fox</td>\n",
       "      <td>6295</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>BMW 320i</td>\n",
       "      <td>9735</td>\n",
       "      <td>25</td>\n",
       "      <td>4</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>Datsun 200</td>\n",
       "      <td>6229</td>\n",
       "      <td>23</td>\n",
       "      <td>4</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>Datsun 210</td>\n",
       "      <td>4589</td>\n",
       "      <td>35</td>\n",
       "      <td>5</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          make  price  mpg  rep78  foreign\n",
       "52   Audi 5000   9690   17      5  Foreign\n",
       "53    Audi Fox   6295   23      3  Foreign\n",
       "54    BMW 320i   9735   25      4  Foreign\n",
       "55  Datsun 200   6229   23      4  Foreign\n",
       "56  Datsun 210   4589   35      5  Foreign"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto[auto.foreign=='Foreign'][['make',\n",
    "                               'price',\n",
    "                               'mpg',\n",
    "                               'rep78',\n",
    "                               'foreign']].head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List Observations missing 1978 Repair Record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list make price mpg rep78 foreign if missing(rep78)\n",
      "\n",
      "     +-------------------------------------------------+\n",
      "     | make             price   mpg   rep78    foreign |\n",
      "     |-------------------------------------------------|\n",
      "  3. | AMC Spirit       3,799    22       .   Domestic |\n",
      "  7. | Buick Opel       4,453    26       .   Domestic |\n",
      " 45. | Plym. Sapporo    6,486    26       .   Domestic |\n",
      " 51. | Pont. Phoenix    4,424    19       .   Domestic |\n",
      " 64. | Peugeot 604     12,990    14       .    Foreign |\n",
      "     +-------------------------------------------------+\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('list make price mpg rep78 foreign if missing(rep78)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>make</th>\n",
       "      <th>price</th>\n",
       "      <th>mpg</th>\n",
       "      <th>rep78</th>\n",
       "      <th>foreign</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AMC Spirit</td>\n",
       "      <td>3799</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Buick Opel</td>\n",
       "      <td>4453</td>\n",
       "      <td>26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>Plym. Sapporo</td>\n",
       "      <td>6486</td>\n",
       "      <td>26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>Pont. Phoenix</td>\n",
       "      <td>4424</td>\n",
       "      <td>19</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>Peugeot 604</td>\n",
       "      <td>12990</td>\n",
       "      <td>14</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             make  price  mpg  rep78   foreign\n",
       "2      AMC Spirit   3799   22    NaN  Domestic\n",
       "6      Buick Opel   4453   26    NaN  Domestic\n",
       "44  Plym. Sapporo   6486   26    NaN  Domestic\n",
       "50  Pont. Phoenix   4424   19    NaN  Domestic\n",
       "63    Peugeot 604  12990   14    NaN   Foreign"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto[auto.rep78.isnull()][['make', 'price', 'mpg', 'rep78', 'foreign']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### One-way Tabs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tabulate foreign\n",
      "\n",
      "   Car type |      Freq.     Percent        Cum.\n",
      "------------+-----------------------------------\n",
      "   Domestic |         52       70.27       70.27\n",
      "    Foreign |         22       29.73      100.00\n",
      "------------+-----------------------------------\n",
      "      Total |         74      100.00\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('tabulate foreign')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Domestic    52\n",
       "Foreign     22\n",
       "dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.foreign.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Crosstabs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tabulate rep78 foreign\n",
      "\n",
      "    Repair |\n",
      "    Record |       Car type\n",
      "      1978 |  Domestic    Foreign |     Total\n",
      "-----------+----------------------+----------\n",
      "         1 |         2          0 |         2 \n",
      "         2 |         8          0 |         8 \n",
      "         3 |        27          3 |        30 \n",
      "         4 |         9          9 |        18 \n",
      "         5 |         2          9 |        11 \n",
      "-----------+----------------------+----------\n",
      "     Total |        48         21 |        69 \n",
      "\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('tabulate rep78 foreign')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>foreign</th>\n",
       "      <th>Domestic</th>\n",
       "      <th>Foreign</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rep78</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>27</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "foreign  Domestic  Foreign\n",
       "rep78                     \n",
       "1               2        0\n",
       "2               8        0\n",
       "3              27        3\n",
       "4               9        9\n",
       "5               2        9"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(auto.rep78, auto.foreign)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### By and Groupby"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "by foreign: summarize\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "-> foreign = Domestic\n",
      "\n",
      "    Variable |       Obs        Mean    Std. Dev.       Min        Max\n",
      "-------------+--------------------------------------------------------\n",
      "        make |         0\n",
      "       price |        52    6072.423    3097.104       3291      15906\n",
      "         mpg |        52    19.82692    4.743297         12         34\n",
      "       rep78 |        48    3.020833     .837666          1          5\n",
      "    headroom |        52    3.153846    .9157578        1.5          5\n",
      "-------------+--------------------------------------------------------\n",
      "       trunk |        52       14.75    4.306288          7         23\n",
      "      weight |        52    3317.115    695.3637       1800       4840\n",
      "      length |        52    196.1346    20.04605        147        233\n",
      "        turn |        52    41.44231    3.967582         31         51\n",
      "displacement |        52    233.7115    85.26299         86        425\n",
      "-------------+--------------------------------------------------------\n",
      "  gear_ratio |        52    2.806538    .3359556       2.19       3.58\n",
      "     foreign |        52           0           0          0          0\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "-> foreign = Foreign\n",
      "\n",
      "    Variable |       Obs        Mean    Std. Dev.       Min        Max\n",
      "-------------+--------------------------------------------------------\n",
      "        make |         0\n",
      "       price |        22    6384.682    2621.915       3748      12990\n",
      "         mpg |        22    24.77273    6.611187         14         41\n",
      "       rep78 |        21    4.285714    .7171372          3          5\n",
      "    headroom |        22    2.613636    .4862837        1.5        3.5\n",
      "-------------+--------------------------------------------------------\n",
      "       trunk |        22    11.40909    3.216906          5         16\n",
      "      weight |        22    2315.909    433.0035       1760       3420\n",
      "      length |        22    168.5455    13.68255        142        193\n",
      "        turn |        22    35.40909    1.501082         32         38\n",
      "displacement |        22    111.2273    24.88054         79        163\n",
      "-------------+--------------------------------------------------------\n",
      "  gear_ratio |        22    3.507273    .2969076       2.98       3.89\n",
      "     foreign |        22           1           0          1          1\n",
      "\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('by foreign: summarize')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>foreign</th>\n",
       "      <th colspan=\"6\" halign=\"left\">Domestic</th>\n",
       "      <th colspan=\"6\" halign=\"left\">Foreign</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>50%</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>50%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>displacement</th>\n",
       "      <td>52</td>\n",
       "      <td>233.711538</td>\n",
       "      <td>85.262993</td>\n",
       "      <td>86.00</td>\n",
       "      <td>231.00</td>\n",
       "      <td>425.00</td>\n",
       "      <td>22</td>\n",
       "      <td>111.227273</td>\n",
       "      <td>24.880537</td>\n",
       "      <td>79.00</td>\n",
       "      <td>101.00</td>\n",
       "      <td>163.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>gear_ratio</th>\n",
       "      <td>52</td>\n",
       "      <td>2.806538</td>\n",
       "      <td>0.335960</td>\n",
       "      <td>2.19</td>\n",
       "      <td>2.75</td>\n",
       "      <td>3.58</td>\n",
       "      <td>22</td>\n",
       "      <td>3.507273</td>\n",
       "      <td>0.296906</td>\n",
       "      <td>2.98</td>\n",
       "      <td>3.61</td>\n",
       "      <td>3.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>headroom</th>\n",
       "      <td>52</td>\n",
       "      <td>3.153846</td>\n",
       "      <td>0.915758</td>\n",
       "      <td>1.50</td>\n",
       "      <td>3.50</td>\n",
       "      <td>5.00</td>\n",
       "      <td>22</td>\n",
       "      <td>2.613636</td>\n",
       "      <td>0.486284</td>\n",
       "      <td>1.50</td>\n",
       "      <td>2.50</td>\n",
       "      <td>3.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>52</td>\n",
       "      <td>196.134615</td>\n",
       "      <td>20.046054</td>\n",
       "      <td>147.00</td>\n",
       "      <td>200.00</td>\n",
       "      <td>233.00</td>\n",
       "      <td>22</td>\n",
       "      <td>168.545455</td>\n",
       "      <td>13.682548</td>\n",
       "      <td>142.00</td>\n",
       "      <td>170.00</td>\n",
       "      <td>193.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mpg</th>\n",
       "      <td>52</td>\n",
       "      <td>19.826923</td>\n",
       "      <td>4.743297</td>\n",
       "      <td>12.00</td>\n",
       "      <td>19.00</td>\n",
       "      <td>34.00</td>\n",
       "      <td>22</td>\n",
       "      <td>24.772727</td>\n",
       "      <td>6.611187</td>\n",
       "      <td>14.00</td>\n",
       "      <td>24.50</td>\n",
       "      <td>41.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>price</th>\n",
       "      <td>52</td>\n",
       "      <td>6072.423077</td>\n",
       "      <td>3097.104279</td>\n",
       "      <td>3291.00</td>\n",
       "      <td>4782.50</td>\n",
       "      <td>15906.00</td>\n",
       "      <td>22</td>\n",
       "      <td>6384.681818</td>\n",
       "      <td>2621.915083</td>\n",
       "      <td>3748.00</td>\n",
       "      <td>5759.00</td>\n",
       "      <td>12990.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rep78</th>\n",
       "      <td>48</td>\n",
       "      <td>3.020833</td>\n",
       "      <td>0.837666</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>5.00</td>\n",
       "      <td>21</td>\n",
       "      <td>4.285714</td>\n",
       "      <td>0.717137</td>\n",
       "      <td>3.00</td>\n",
       "      <td>4.00</td>\n",
       "      <td>5.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trunk</th>\n",
       "      <td>52</td>\n",
       "      <td>14.750000</td>\n",
       "      <td>4.306288</td>\n",
       "      <td>7.00</td>\n",
       "      <td>16.00</td>\n",
       "      <td>23.00</td>\n",
       "      <td>22</td>\n",
       "      <td>11.409091</td>\n",
       "      <td>3.216906</td>\n",
       "      <td>5.00</td>\n",
       "      <td>11.00</td>\n",
       "      <td>16.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>turn</th>\n",
       "      <td>52</td>\n",
       "      <td>41.442308</td>\n",
       "      <td>3.967582</td>\n",
       "      <td>31.00</td>\n",
       "      <td>42.00</td>\n",
       "      <td>51.00</td>\n",
       "      <td>22</td>\n",
       "      <td>35.409091</td>\n",
       "      <td>1.501082</td>\n",
       "      <td>32.00</td>\n",
       "      <td>36.00</td>\n",
       "      <td>38.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>52</td>\n",
       "      <td>3317.115385</td>\n",
       "      <td>695.363740</td>\n",
       "      <td>1800.00</td>\n",
       "      <td>3360.00</td>\n",
       "      <td>4840.00</td>\n",
       "      <td>22</td>\n",
       "      <td>2315.909091</td>\n",
       "      <td>433.003454</td>\n",
       "      <td>1760.00</td>\n",
       "      <td>2180.00</td>\n",
       "      <td>3420.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "foreign      Domestic                                                        \\\n",
       "                count         mean          std      min      50%       max   \n",
       "displacement       52   233.711538    85.262993    86.00   231.00    425.00   \n",
       "gear_ratio         52     2.806538     0.335960     2.19     2.75      3.58   \n",
       "headroom           52     3.153846     0.915758     1.50     3.50      5.00   \n",
       "length             52   196.134615    20.046054   147.00   200.00    233.00   \n",
       "mpg                52    19.826923     4.743297    12.00    19.00     34.00   \n",
       "price              52  6072.423077  3097.104279  3291.00  4782.50  15906.00   \n",
       "rep78              48     3.020833     0.837666     1.00     3.00      5.00   \n",
       "trunk              52    14.750000     4.306288     7.00    16.00     23.00   \n",
       "turn               52    41.442308     3.967582    31.00    42.00     51.00   \n",
       "weight             52  3317.115385   695.363740  1800.00  3360.00   4840.00   \n",
       "\n",
       "foreign      Foreign                                                        \n",
       "               count         mean          std      min      50%       max  \n",
       "displacement      22   111.227273    24.880537    79.00   101.00    163.00  \n",
       "gear_ratio        22     3.507273     0.296906     2.98     3.61      3.89  \n",
       "headroom          22     2.613636     0.486284     1.50     2.50      3.50  \n",
       "length            22   168.545455    13.682548   142.00   170.00    193.00  \n",
       "mpg               22    24.772727     6.611187    14.00    24.50     41.00  \n",
       "price             22  6384.681818  2621.915083  3748.00  5759.00  12990.00  \n",
       "rep78             21     4.285714     0.717137     3.00     4.00      5.00  \n",
       "trunk             22    11.409091     3.216906     5.00    11.00     16.00  \n",
       "turn              22    35.409091     1.501082    32.00    36.00     38.00  \n",
       "weight            22  2315.909091   433.003454  1760.00  2180.00   3420.00  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.groupby(by=auto.foreign).describe(percentiles=[]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Mean By Category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tabulate foreign, summarize(mpg)\n",
      "\n",
      "            |      Summary of Mileage (mpg)\n",
      "   Car type |        Mean   Std. Dev.       Freq.\n",
      "------------+------------------------------------\n",
      "   Domestic |   19.826923   4.7432972          52\n",
      "    Foreign |   24.772727   6.6111869          22\n",
      "------------+------------------------------------\n",
      "      Total |   21.297297   5.7855032          74\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('tabulate foreign, summarize(mpg)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "foreign\n",
       "Domestic    19.826923\n",
       "Foreign     24.772727\n",
       "Name: mpg, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.groupby(by=auto.foreign)['mpg'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Correlation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "correlate mpg weight\n",
      "(obs=74)\n",
      "\n",
      "             |      mpg   weight\n",
      "-------------+------------------\n",
      "         mpg |   1.0000\n",
      "      weight |  -0.8072   1.0000\n",
      "\n",
      "\n",
      ". "
     ]
    }
   ],
   "source": [
    "stata.write('correlate mpg weight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.80717485894244212"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto.mpg.corr(auto.weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>mpg</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.807175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>-0.807175</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             mpg    weight\n",
       "mpg     1.000000 -0.807175\n",
       "weight -0.807175  1.000000"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auto[['mpg','weight']].corr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Python Language Features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data Types: Lists and Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Stata', 'Stephen Childs')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "software = ['Stata', 'Python'] # list\n",
    "person = {'name': 'Stephen Childs',\n",
    "          'employer': 'University of Calgary',\n",
    "          'city': 'Calgary',\n",
    "          'province': 'Alberta'} #dictionary\n",
    "software[0], person['name']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "source": [
    "Python has some very useful and powerful language features. A good starting point is to look at some of the data structures built into the language. You can think of a Python list as a Stata macro list. In Python, lists can contain any type of object and can even contain different types in the same list. Lists are _ordered_.\n",
    "\n",
    "Dictionaries are a very powerful data type. It lets you define a set of **keys** and related **values**. This is a very powerful and flexible data structure. The values are _unordered_."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "def say_hello():\n",
    "    print('Hello world!')\n",
    "\n",
    "say_hello()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def double(x):\n",
    "    return x*2\n",
    "\n",
    "double(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `lambda` Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(lambda x: x**2)(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    4\n",
       "5    5\n",
       "6    6\n",
       "7    7\n",
       "dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = pd.Series(range(8))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     0\n",
       "1     1\n",
       "2     4\n",
       "3     9\n",
       "4    16\n",
       "5    25\n",
       "6    36\n",
       "7    49\n",
       "dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.apply(lambda x: x**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Glossary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<table>\n",
    "    <tr>\n",
    "        <th>Stata</th>\n",
    "        <th>Python</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>macro</td>\n",
    "        <td>variable</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>macro list</td>\n",
    "        <td>list</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>variable</td>\n",
    "        <td><code>pd.Series</code> or a column of a <code>pd.DataFrame</code></td>\n",
    "    </tr>\n",
    "        <tr>\n",
    "        <td>data</td>\n",
    "        <td><code>pd.DataFrame</code></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><code>.dta</code> file</td>\n",
    "        <td><ul>\n",
    "            <li><a href=\"https://docs.python.org/3/library/pickle.html\">Pickle</a> (though <a href=\"https://www.youtube.com/watch?v=7KnfGDajDQw\">be careful!</a>)</li>\n",
    "            <li><code>csv</code></li>\n",
    "            <li><a href=\"https://www.hdfgroup.org/HDF5/\">HDF5</a> (<a href=\"www.h5py.org\">Python package</a>)</li>\n",
    "            <li>etc&hellip;</li\n",
    "        </ul></td>\n",
    "    </tr>\n",
    "        <tr>\n",
    "        <td><code>ssc</code></td>\n",
    "        <td><code>pip</code></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><a href=\"https://ideas.repec.org/s/boc/bocode.html\">Boston College Statistical Software Components (SSC) archive</a></td>\n",
    "        <td><a href=\"https://pypi.python.org/pypi\">PyPI - the Python Package Index</a> (<a href=\"https://www.youtube.com/watch?v=PPN3KTtrnZM\">\"The Cheeseshop\"</a>)</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><code>program</code></td>\n",
    "        <td>function</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><code>do</code> file</td>\n",
    "        <td>module (<code>.py</code> file)</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><code>ado</code> file</td>\n",
    "        <td>package</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Workflows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### ETL/Data Cleaning"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* **advantages:**\n",
    "    * wide variety of tools already available\n",
    "    * create new data cleaning functions\n",
    "* use pandas to prepare the data\n",
    "    * organize code in separate files\n",
    "    * move data into Stata when analysis file is ready"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"Recode the PSIS Gender variable.\n",
    "\n",
    "Input\n",
    "-----\n",
    "sd.raw.Gender.pd\n",
    "\n",
    "Output\n",
    "------\n",
    "sd.Gender.pd\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "import epandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from eppdoc import eppdoc\n",
    "\n",
    "# Load series.\n",
    "d = eppdoc(__doc__)\n",
    "s = d.read()\n",
    "\n",
    "# Change to full labels.\n",
    "s = s.replace({'F': 'Female', 'M': 'Male', np.nan: 'N/A'})\n",
    "\n",
    "# Categorize and Order\n",
    "s = s.astype('category')\n",
    "s = s.cat.reorder_categories(['Female', 'Male', 'N/A'])\n",
    "\n",
    "# Pickle.\n",
    "d.writes(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "source": [
    "e.g. You have a data source - a set of files - it is easy to write a Python sccript to turn that set of files into your analysis files."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Stata Code Generation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "source": [
    "* a less \"fiddly\" replacement for macros (see the `format` method above)\n",
    "* deal with repitition and patterns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "vars = ['mpg', 'rep78', 'headroom', 'trunk', 'weight',\n",
    "        'length', 'turn', 'displacement',\n",
    "        'gear_ratio', 'i.foreign']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from itertools import combinations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "regress price mpg rep78\n",
      "regress price mpg headroom\n",
      "regress price mpg trunk\n",
      "regress price mpg weight\n",
      "regress price mpg length\n",
      "regress price mpg turn\n",
      "regress price mpg displacement\n",
      "regress price mpg gear_ratio\n",
      "regress price mpg i.foreign\n",
      "regress price rep78 headroom\n",
      "regress price rep78 trunk\n",
      "regress price rep78 weight\n",
      "regress price rep78 length\n",
      "regress price rep78 turn\n",
      "regress price rep78 displacement\n",
      "regress price rep78 gear_ratio\n",
      "regress price rep78 i.foreign\n",
      "regress price headroom trunk\n",
      "regress price headroom weight\n",
      "regress price headroom length\n",
      "regress price headroom turn\n",
      "regress price headroom displacement\n",
      "regress price headroom gear_ratio\n",
      "regress price headroom i.foreign\n",
      "regress price trunk weight\n",
      "regress price trunk length\n",
      "regress price trunk turn\n",
      "regress price trunk displacement\n",
      "regress price trunk gear_ratio\n",
      "regress price trunk i.foreign\n",
      "regress price weight length\n",
      "regress price weight turn\n",
      "regress price weight displacement\n",
      "regress price weight gear_ratio\n",
      "regress price weight i.foreign\n",
      "regress price length turn\n",
      "regress price length displacement\n",
      "regress price length gear_ratio\n",
      "regress price length i.foreign\n",
      "regress price turn displacement\n",
      "regress price turn gear_ratio\n",
      "regress price turn i.foreign\n",
      "regress price displacement gear_ratio\n",
      "regress price displacement i.foreign\n",
      "regress price gear_ratio i.foreign\n"
     ]
    }
   ],
   "source": [
    "for x in combinations(vars, 2):\n",
    "    print('regress price {vars}'.format(vars=' '.join(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Processing Stata Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from epstata import Stpy\n",
    "import predict_models\n",
    "\n",
    "def predict_test(a, filename, model, key, iteration, results):\n",
    "    a.write('use \"{filename}\", clear'.format(filename=filename))\n",
    "    a.write('generate phat = .')\n",
    "    tmp1 = NamedTemporaryFile(suffix='.dta')\n",
    "    a.write('save {name}, replace'.format(name=tmp1.name))\n",
    "    a.write('capture drop phat')\n",
    "    a.write('set seed {iteration}'.format(iteration=iteration))\n",
    "    a.write('generate cut = runiform()')\n",
    "    a.write('logit Leaver {model} if cut < .5, iterate(200)'.format(\n",
    "            model=model))\n",
    "    a.write('predict phat if cut >= .5')\n",
    "    a.write('keep if cut >= .5')\n",
    "    a.write('keep ID Leaver phat')\n",
    "    a.write('save {name}, replace'.format(name=tmp1.name))\n",
    "    df = pd.read_stata(tmp1.name)\n",
    "    df.set_index(df['ID'], inplace=True)\n",
    "                df['Log Loss'] = -1 * (df['Leaver']*np.log(df['phat'])\n",
    "            + (1-df['Leaver'])*np.log(1-df['phat']))\n",
    "    results.write('{key} {iteration}: Log Loss: {logl}\\n'.format(\n",
    "            key=key,\n",
    "            iteration=iteration,\n",
    "            logl=df['Log Loss'].mean()))\n",
    "    return df['Log Loss']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conclusion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* only an introduction to Python meant to whet your appitite\n",
    "* show some possibilites\n",
    "* Stata/Python integration is still a work in progress\n",
    "* allow you to mix and match - replace part of your workflow with Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Zen of Python, by Tim Peters\n",
      "\n",
      "Beautiful is better than ugly.\n",
      "Explicit is better than implicit.\n",
      "Simple is better than complex.\n",
      "Complex is better than complicated.\n",
      "Flat is better than nested.\n",
      "Sparse is better than dense.\n",
      "Readability counts.\n",
      "Special cases aren't special enough to break the rules.\n",
      "Although practicality beats purity.\n",
      "Errors should never pass silently.\n",
      "Unless explicitly silenced.\n",
      "In the face of ambiguity, refuse the temptation to guess.\n",
      "There should be one-- and preferably only one --obvious way to do it.\n",
      "Although that way may not be obvious at first unless you're Dutch.\n",
      "Now is better than never.\n",
      "Although never is often better than *right* now.\n",
      "If the implementation is hard to explain, it's a bad idea.\n",
      "If the implementation is easy to explain, it may be a good idea.\n",
      "Namespaces are one honking great idea -- let's do more of those!\n"
     ]
    }
   ],
   "source": [
    "import this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"1250\"\n",
       "            height=\"450\"\n",
       "            src=\"http://www.pyohio.org/\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x10a8b8630>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IFrame(src='http://www.pyohio.org/', width=1250, height=450)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Brandon Rhodes [pandas tutorial](https://github.com/brandon-rhodes/pycon-pandas-tutorial) - PyCon Montréal 2015"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Contact"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "* The notebook will be up on github: [https://github.com/sechilds/stataconf2015](https://github.com/sechilds/stataconf2015)\n",
    "    * You can see it rendered on [nbviewer](http://nbviewer.ipython.org/github/sechilds/stataconf2015/blob/master/StataConf2015.ipynb)\n",
    "* Twitter: [@sechilds](https://twitter.com/sechilds)\n",
    "* E-mail: [Stephen.Childs@ucalgary.ca](mailto:Stephen.Childs@uCalgary.ca)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
